package com.hddz.acquisition.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

public class DialogHelper {

	private ProgressDialog mProgress;

	private AlertDialog.Builder mAlert;
	private Context context;

	private Boolean isShowAlter = false;

	public Boolean getIsShowAlter() {
		return isShowAlter;
	}

	public void setIsShowAlter(Boolean isShowAlter) {
		this.isShowAlter = isShowAlter;
	}

	public DialogHelper(Context context) {
		mProgress = new ProgressDialog(context);
		mProgress.getWindow().setGravity(Gravity.CENTER);
		this.context = context;
		mAlert = new AlertDialog.Builder(context);
	}
	
	public void showAlertDialog(String message, int textsize) {
		mProgress.dismiss();
		mAlert = new AlertDialog.Builder(context);
		mAlert.setMessage(message).setPositiveButton("确定", null).setTitle("");
		setDialogFontSize(mAlert.show(), textsize);
	}

	public void showAlertDialog(String message, int textsize, OnClickListener listener) {
		mProgress.dismiss();
		mAlert = new AlertDialog.Builder(context);
		mAlert.setMessage(message).setNegativeButton("确定", listener).setTitle("").setPositiveButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				isShowAlter = false;
			}
		});
		setDialogFontSize(mAlert.show(), textsize);
	}

	public void showProgressDialog(String message, boolean cancel, int textsize) {
		// TODO Auto-generated method stub
		mProgress.setMessage(message);
		mProgress.setCancelable(cancel);
		mProgress.getWindow().setGravity(Gravity.CENTER);
		mProgress.setOnCancelListener(null);
		mProgress.show();
		setDialogFontSize(mProgress, textsize);
	}
	
	public void showProgressDialog(String message, boolean cancel, int textsize,int gravity) {
		// TODO Auto-generated method stub
		mProgress.setMessage(message);
		mProgress.setCancelable(cancel);
		mProgress.getWindow().setGravity(gravity);
		mProgress.setOnCancelListener(null);
		mProgress.show();
		setDialogFontSize(mProgress, textsize);
	}

	public void closeProgressDialog() {
		mProgress.dismiss();
	}

	private void setDialogFontSize(Dialog dialog, int size) {
		Window window = dialog.getWindow();
		View view = window.getDecorView();
		setViewFontSize(view, size);
	}

	private void setViewFontSize(View view, int size) {
		if (view instanceof ViewGroup) {
			ViewGroup parent = (ViewGroup) view;
			int count = parent.getChildCount();
			for (int i = 0; i < count; i++) {
				setViewFontSize(parent.getChildAt(i), size);
			}
		} else if (view instanceof TextView) {
			TextView textview = (TextView) view;
			textview.setTextSize(size);
		}
	}
}
